package com.yc.fresh.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yc.fresh.bean.GoodsInfo;
import com.yc.fresh.mapper.IGoodsInfoMapper;
import com.yc.fresh.mapper.ITypeInfoMapper;
import com.yc.fresh.service.IGoodsInfoService;
import com.yc.fresh.util.StringUtil;

/**
 * 商品信息业务模型层的实现
 * @author H
 * @date 2021年7月16日
 */
@Service
public class GoodsInfoServiceImpl implements IGoodsInfoService{
	@Autowired
	private IGoodsInfoMapper goodsInfoMapper;
	
	@Autowired
	private ITypeInfoMapper typeInfoMapper;
	
	@Override
	public int add(GoodsInfo gf) {
		if(StringUtil.checkNull(gf.getGname(), gf.getPics(), gf.getIntro())) {
			return -1;
		}
		return goodsInfoMapper.add(gf);
	}

	@Override
	public Map<String, Object> findIndex() {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("goods", goodsInfoMapper.findIndex());// 查每个类型的前四个商品
		map.put("types", typeInfoMapper.finds());// 查所有的可用的类型信息
		return map;
	}

	@Override
	public List<GoodsInfo> findByType(String tno, int page, int rows) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("tno",tno);
		map.put("page", (page - 1)*rows);
		map.put("rows",rows);
		return goodsInfoMapper.findByType(map);
	}

	@Override
	public Map<String, Object> findByFirsh(String tno, int page, int rows) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("total", goodsInfoMapper.total(tno));
		map.put("rows", this.findByType(tno, page, rows));
		return map;
	}

	@Override
	public GoodsInfo findByGno(String gno) {
		// TODO Auto-generated method stub
		return goodsInfoMapper.findByGno(gno);
	}

	@Override
	public int updateStores(String[] cnos) {
		// TODO Auto-generated method stub
		if(cnos == null || cnos.length <= 0) {
			return 0;
		}
		return goodsInfoMapper.updateStores(cnos);
	}

	@Override
	public int updateStore(Map<String, Object> map) {
		// TODO Auto-generated method stub
		if(map == null || map.isEmpty()) {
			return 0;
		}
		return goodsInfoMapper.updateStore(map);
	}

}
